package tests.第三方工具类测试使用.mapstruct;

import lombok.Data;
import lombok.ToString;
import org.junit.Test;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: SunAo
 * @create: 2020-09-08 10:55
 */
public class ComplexObjectTrans {

    @Data
    static class Source{
        private String name;
        private String pass;
        private List<School> schools;
    }
    @Data
    static class School{
        private String name;
        private String level;
        private Address address;
    }
    @Data
    static class Address{
        private String country;
        private String city;
    }

    @ToString
    @Data
    static class Target{
        private String name;
        private String password;
        private List<SchooleInfo> schooleInfos;
    }
    @Data
    static class SchooleInfo{
        private String name;
        private String level;
        private String country;
        private String city;
    }

    @Mapper
    interface Convert{
        Convert INSTANCE = Mappers.getMapper(Convert.class);

//        @Mappings({
//                @Mapping(source="pass",target = "password"),
//                @Mapping(source="schools.name",target = "schooleInfos.name"),
//                @Mapping(source="schools.level",target = "schooleInfos.level"),
//                @Mapping(source="schools.address.country",target = "schooleInfos.country"),
//                @Mapping(source="schools.address.city",target = "schooleInfos.city"),
//        })
        Target to(Source source);
    }


    static Source source = new Source();
    static{
        source.setName("孙傲");
        source.setPass("yunbin");
        School school1 = new School();
        Address address1 = new Address();
        school1.setName("邓州市第二高级中学");
        school1.setLevel("第二高级");
        address1.setCountry("中国");
        address1.setCity("邓州市");
        school1.setAddress(address1);
        School school2 = new School();
        Address address2 = new Address();
        school2.setName("南阳市第一高级中学");
        school2.setLevel("第一高级");
        address2.setCountry("中国");
        address2.setCity("南阳市");
        school2.setAddress(address2);
        source.setSchools(new ArrayList<>());
        source.getSchools().add(school1);
        source.getSchools().add(school2);
    }


    @Test
    public void to(){
        Target t = Convert.INSTANCE.to(source);
        System.out.println(t.toString());
    }

    // 内置List无法进行转换

}
